﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PeonyShop.Bussiness;
using PeonyShop.Class;
using PenoyShop.Utilities;

namespace PeonyShop.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string sPassHash = EncryptPassword("123456");
            //Logger.FrameworkLogger.Info(sPassHash);
            if (Session["Admin"] != null)
            {
                Response.Redirect("Defaul.aspx");
            }
           
        }

       
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string sUsername = txtUsername.Text.Trim();
            string sPassword = txtPassword.Text.Trim();
            string sPassHash = EncryptPassword(sPassword);
            BCustomer bBcustomer = new BCustomer();
            bool bResLogin = bBcustomer.LoginAdmin(sUsername, sPassHash,1);

            if (bResLogin)
            {
                CCustomer oCus = new CCustomer();
                oCus.UserN = sUsername;
                oCus.Password = sPassword;
                oCus.RoleId = 1;
                Session["Admin"] = oCus;
                Response.Redirect("Default.aspx");

            }
            else
                spRes.Text = "Username or password incorrect";

        }
        
        public string EncryptPassword(string sPassword)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashedDataBytes = md5Hasher.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(sPassword));
            string sEncryptPass = Convert.ToBase64String(hashedDataBytes);
            return sEncryptPass;
        }

    }
}